 package com.sdemo.modules.sys.service.impl;


import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sdemo.modules.sys.mapper.DeptMapper;
import com.sdemo.modules.sys.model.Dept;
import com.sdemo.modules.sys.model.TreeDept;
import com.sdemo.modules.sys.service.DeptService;

@Service
public class DeptServiceImpl implements DeptService {
	
	@Autowired
	private DeptMapper deptMapper;
	
	@Override
	public List<Dept> queryDept() {
		// TODO Auto-generated method stub
		return deptMapper.queryDept();
	}

	@Override
	public int insertDept(Dept dept) {
		// TODO Auto-generated method stub
		return deptMapper.insertDept(dept);
	}

	@Override
	public List<TreeDept> queryDeptByUserId(Dept dept) {
		List<TreeDept> deptList=deptMapper.queryDeptById(dept);//dept.id
		List<TreeDept> tempList = new ArrayList<>();
		ztreeDept(deptList,tempList);
		deptList.addAll(tempList);
		return deptList;
	}
	
	public void ztreeDept(List<TreeDept> deptList,List<TreeDept> tempList){
		for(TreeDept dept:deptList){
			Dept temp = new Dept();
			temp.setPid(dept.getId());
			List<TreeDept> ztreeList = deptMapper.queryDeptById(temp);//pid
			if(null!=ztreeList && ztreeList.size()>0){
				tempList.addAll(ztreeList);
				ztreeDept(ztreeList,tempList);
			}
		}
	}
}
